﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using CP_SchoolForChildren_v0._1.Controllers.Support;
using CP_SchoolForChildren_v0._1.Models.DataModel;
using EntityFramework.Extensions;

namespace CP_SchoolForChildren_v0._1.Controllers.BE
{
    [Authorize(Roles = "Admin")]
    public class SchoolManagementController : Controller
    {
        FunctionController fc = new FunctionController();
        SFCMessage sfcMessage = new SFCMessage();

        #region List Action

        [Authorize(Roles = "Admin")]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Type()
        {
            return View();
        }

        [Authorize(Roles = "Admin")]
        public ActionResult City()
        {
            return View();
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Comment()
        {
            return View();
        }

        [Authorize(Roles = "Admin")]
        public ActionResult ReportedSchool()
        {
            return View();
        }

        #endregion

        #region School action
        [Authorize(Roles = "Admin")]
        [HttpPost]
        public JsonResult ListSchool(string keywords, int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                if (keywords == null)
                    keywords = "";
                var record = from sc in fc.db.Schools
                             join st in fc.db.SchoolTypes on sc.TypeID equals st.TypeID
                             join dis in fc.db.Districts on sc.DistrictID equals dis.DistrictID
                             join ct in fc.db.Cities on dis.CityID equals ct.CityID
                             join sr in fc.db.SystemRatings on sc.SchoolID equals sr.SchoolID
                             where sc.SchoolName.Contains(keywords)
                             select new
                             {
                                 SchoolID = sc.SchoolID,
                                 SchoolName = sc.SchoolName,
                                 TypeID = sc.TypeID,
                                 TypeName = st.TypeName,
                                 Address = sc.Address,
                                 DistrictID = sc.DistrictID,
                                 DistrictName = dis.DistrictName,
                                 CityID = ct.CityID,
                                 CityName = ct.CityName,
                                 Phone = sc.Phone,
                                 Website = sc.Website,
                                 Email = sc.Email,
                                 Principal = sc.Principal,
                                 Program = sc.Program,
                                 StandardSS = sc.StandardSS,
                                 Area = sc.Area,
                                 EnrollmentTime = sc.EnrollmentTime,
                                 AcceptAgeDescription = sc.AcceptAgeDescription,
                                 Admission = sc.Admission,
                                 EstablishYear = sc.EstablishYear,
                                 NumberOfTeacher = sc.NumberOfTeacher,
                                 NumberOfChildren = sc.NumberOfTeacher,
                                 TuitionFee = sc.TuitionFee,
                                 GoogleAPI = sc.GoogleAPI,
                                 Description = sc.Description,
                                 Rating = sr.SystemRate
                             };
                var totalrow = record.Count();

                var temp = record.OrderUsingSortExpression(jtSorting).
                     Skip(jtStartIndex).Take(jtPageSize);

                return Json(new { Result = "OK", Records = temp.ToList(), TotalRecordCount = totalrow });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [Authorize(Roles = "Admin")]
        [HttpPost]
        public JsonResult CreateSchool(School school, List<int> ServiceList)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Form is not valid! " +
                        "Please correct it and try again."
                    });
                }
                    fc.db.Schools.Add(school);
                    fc.db.SaveChanges();

                    var sid = school.SchoolID;
                    if (ServiceList != null)
                    {
                        for (int i = 0; i < ServiceList.Count; i++)
                        {
                            var scsvr = new SchoolService()
                                            {
                                                SchoolID = sid,
                                                ServiceID = ServiceList.ElementAt(i)
                                            };
                            fc.db.Entry(scsvr).State = System.Data.EntityState.Added;
                        }
                    }

                    fc.RankSchool(sid);
                    var record = fc.db.SaveChanges();
                    
                    return Json(new { Result = "OK", Record = record});
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.ToString() });
            }
        }

        [Authorize(Roles = "Admin")]
        [HttpPost]
        public JsonResult UpdateSchool(School school, List<int> ServiceList)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." });
                }
                    fc.db.Entry(school).State = System.Data.EntityState.Modified;
                    fc.db.SaveChanges();

                    var sid = school.SchoolID;
                    if (ServiceList != null)
                    {
                        fc.db.SchoolServices.Delete(s => s.SchoolID == sid);
                        for (int i = 0; i < ServiceList.Count; i++)
                        {
                            var scsvr = new SchoolService()
                                            {
                                                SchoolID = sid,
                                                ServiceID = ServiceList.ElementAt(i)
                                            };
                            fc.db.Entry(scsvr).State = System.Data.EntityState.Added;
                        }
                    }

                    fc.RankSchool(sid);
                    var record = fc.db.SaveChanges();
                    return Json(new { Result = "OK", Record = record });
                    
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [Authorize(Roles = "Admin")]
        [HttpPost]
        public JsonResult DeleteSchool(School school)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." });
                }
                    DeleteSchoolFK(school);
                    fc.db.Entry(school).State = System.Data.EntityState.Deleted;
                    var record = fc.db.SaveChanges();
                    return Json(new { Result = "OK", Record = record });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [Authorize(Roles = "Admin")]
        public void DeleteSchoolFK(School school)
        {
            try
            {
                fc.db.Comments.Delete(tmp => tmp.SchoolID == school.SchoolID);
                fc.db.FavoriteLists.Delete(tmp => tmp.SchoolID == school.SchoolID);
                fc.db.SchoolServices.Delete(tmp => tmp.SchoolID == school.SchoolID);
                fc.db.SocialRatings.Delete(tmp => tmp.SchoolID == school.SchoolID);
                fc.db.SystemRatings.Delete(tmp => tmp.SchoolID == school.SchoolID);
                fc.db.ReportSchools.Delete(tmp => tmp.SchoolID == school.SchoolID);
            }catch(Exception ex)
            {
                
            }
        }

        [Authorize(Roles = "Admin")]
        public void AddService(School school)
        {
            try
            {
                // Social Rate
                var socialrate = new SocialRating
                                     {
                                         SchoolID = school.SchoolID,
                                         UserID = 1,
                                         SocialRate = 0.0
                                     };
                fc.db.SocialRatings.Add(socialrate);
            }catch(Exception ex)
            {
                Console.Write(ex.ToString());
            }

        }
        #endregion

        #region SchoolType action
        [HttpPost]
        public JsonResult ListSchoolType(string keywords, int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                if (keywords == null)
                    keywords = "";
                var record = from st in fc.db.SchoolTypes
                             where st.TypeID != 1
                             where st.TypeName.Contains(keywords)
                             select new
                                        {
                                            TypeID = st.TypeID,
                                            TypeName = st.TypeName,
                                            SchoolCount = st.Schools.Count
                                        };
                    
                var totalrow = record.Count();

                var temp = record.OrderUsingSortExpression(jtSorting).
                     Skip(jtStartIndex).Take(jtPageSize);

                return Json(new { Result = "OK", Records = temp.ToList(), TotalRecordCount = totalrow });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [HttpPost]
        public JsonResult CreateSchoolType(SchoolType schooltype)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Form is not valid! " +
                        "Please correct it and try again."
                    });
                }
                if (!CheckNewSchoolType(schooltype))
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Dữ liệu đã tồn tại"
                    });
                }  
                    var record = fc.db.SchoolTypes.Add(schooltype);
                    fc.db.SaveChanges();
                    return Json(new { Result = "OK", Record = record });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [HttpPost]
        public JsonResult UpdateSchoolType(SchoolType schooltype)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." });
                }
                if (!CheckSchoolType(schooltype))
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Dữ liệu đã tồn tại"
                    });
                }  
                    var record = fc.db.SaveChanges();
                    return Json(new { Result = "OK", Record = record });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [HttpPost]
        public JsonResult DeleteSchoolType(SchoolType schooltype)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." });
                }
                if (CheckSchoolTypeFK(schooltype))
                {
                    return Json(new { Result = "ERROR", Message = sfcMessage.ForeginKey });
                }
                    fc.db.Entry(schooltype).State = System.Data.EntityState.Deleted;
                    var record = fc.db.SaveChanges();
                    return Json(new { Result = "OK", Record = record });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        public bool CheckSchoolTypeFK(SchoolType schooltype)
        {
            var st = fc.db.Schools.Where(sc => sc.TypeID == schooltype.TypeID).ToList();
            if (st.Any())
                return true;
            return false;
        }

        public bool CheckSchoolType(SchoolType schooltype)
        {
            var record = fc.db.SchoolTypes.Where(c => c.TypeID != schooltype.TypeID).ToList();
            if (record.Any(st => st.TypeName == schooltype.TypeName))
                return false;
            return true;
        }

        public bool CheckNewSchoolType(SchoolType schooltype)
        {
            var record = fc.db.SchoolTypes.Where(st => st.TypeName == schooltype.TypeName).ToList();
            if (record.Any())
                return false;
            return true;
        }


        #endregion

        #region City action
        [HttpPost]
        public JsonResult ListCity(string keywords, int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                if (keywords == null)
                    keywords = "";
                var record = from c in fc.db.Cities
                             where c.CityID != 1
                             where c.CityName.Contains(keywords)
                             select new
                                            {
                                                CityID = c.CityID,
                                                CityName = c.CityName,
                                                DistrictCount = c.Districts.Count
                                            };
                
                var totalrow = record.Count();

                var temp = record.OrderUsingSortExpression(jtSorting).
                     Skip(jtStartIndex).Take(jtPageSize);

                return Json(new { Result = "OK", Records = temp.ToList(), TotalRecordCount = totalrow });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [HttpPost]
        public JsonResult CreateCity(City city)
        {
            try
            {
                fc.db.Configuration.ProxyCreationEnabled = false;
                if (!ModelState.IsValid)
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Dữ liệu nhập vào không đúng"                        
                    });
                }
                if (!CheckNewCity(city))
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Dữ liệu đã tồn tại"
                    });
                }                                
                    var record = fc.db.Cities.Add(city);
                    fc.db.SaveChanges();
                    return Json(new { Result = "OK", Record = record });

            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [HttpPost]
        public JsonResult UpdateCity(City city)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." });
                }
                if (!CheckCity(city))
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Dữ liệu đã tồn tại"
                    });
                }
                    fc.db.Entry(city).State = System.Data.EntityState.Modified;
                    fc.db.SaveChanges();
                    return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [HttpPost]
        public JsonResult DeleteCity(City city)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." });
                }
                if(CheckCityFK(city))
                {
                    return Json(new { Result = "ERROR", Message = sfcMessage.ForeginKey });
                }
                    fc.db.Entry(city).State = System.Data.EntityState.Deleted;
                    fc.db.SaveChanges();
                    return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }
        //Check new city input
        public bool CheckNewCity(City city)
        {
            var record = fc.db.Cities.Where(c => c.CityName == city.CityName).ToList();
            if (record.Any())
                return false;
            return true;
        }
        //Check city update
        public bool CheckCity(City city)
        {
            var record = fc.db.Cities.Where(c => c.CityID != city.CityID).ToList();
            if (record.Any(c => c.CityName == city.CityName))
                return false;
            return true;
        }
        //Check city FK
        public bool CheckCityFK(City city)
        {
                var st = fc.db.Districts.Where(d => d.CityID == city.CityID).ToList();
                if (st.Any())
                    return true;
                return false;
        }
        #endregion

        #region District action
        [HttpPost]
        public JsonResult ListDistrict(int cityID, string keywords, int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                if (keywords == null)
                    keywords = "";
                var record = from ds in fc.db.Districts
                             where ds.CityID == cityID
                             where ds.DistrictID != 1
                             where ds.DistrictName.Contains(keywords)
                             select new
                                        {
                                            CityID = ds.CityID,
                                            DistrictID = ds.DistrictID,
                                            DistrictName = ds.DistrictName,
                                            SchoolCount = ds.Schools.Count
                                        };
                var totalrow = record.Count();

                var temp = record.OrderUsingSortExpression(jtSorting).
                     Skip(jtStartIndex).Take(jtPageSize);

                return Json(new { Result = "OK", Records = temp.ToList(), TotalRecordCount = totalrow });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [HttpPost]
        public JsonResult CreateDistrict(District district)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Form is not valid! " +
                        "Please correct it and try again."
                    });
                }
                if (!CheckNewDistrict(district))
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Dữ liệu đã tồn tại"
                    });
                }  
                var record = fc.db.Districts.Add(district);
                fc.db.SaveChanges();
                return Json(new { Result = "OK", Record = record });

            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [HttpPost]
        public JsonResult UpdateDistrict(District district)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." });
                }
                if (CheckDistrict(district))
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Dữ liệu đã tồn tại"
                    });
                }
                fc.db.Entry(district).State = System.Data.EntityState.Modified;
                fc.db.SaveChanges();
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }
        [HttpPost]
        public JsonResult DeleteDistrict(District district)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." });
                }
                if (CheckDistrictFK(district))
                {
                    return Json(new { Result = "ERROR", Message = sfcMessage.ForeginKey });
                }
                fc.db.Entry(district).State = System.Data.EntityState.Deleted;
                fc.db.SaveChanges();
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        public bool CheckDistrict(District district)
        {
            var record = fc.db.Districts.Where(d => d.DistrictID != district.DistrictID).
                Where(d => d.CityID == district.CityID)
                .ToList();
            if (record.Any())
                return false;
            return true;
        }

        public bool CheckNewDistrict(District district)
        {
            var record = fc.db.Districts.Where(d => d.CityID == district.CityID).
                Where(dt => dt.DistrictName == district.DistrictName).ToList();
            if (record.Any())
                return false;
            return true;
        }

        public bool CheckDistrictFK(District district)
        {
                var st = fc.db.Schools.Where(s => s.DistrictID == district.DistrictID).ToList();
                if (st.Any())
                    return true;
            return false;
        }

        #endregion

        #region Comment action
        [HttpPost]
        public JsonResult ListComment(string keywords, int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                using (var db = new SFCEntities())
                {
                    if (keywords == null)
                        keywords = "";

                    var record = from cm in fc.db.Comments
                                 join us in fc.db.Users on cm.UserID equals us.UserID
                                 join sc in fc.db.Schools on cm.SchoolID equals sc.SchoolID
                                 where cm.CommentContent.Contains(keywords)
                                 select new
                                 {
                                     CommentID = cm.CommentID,
                                     CommentContent = cm.CommentContent,
                                     CreatedTime = cm.CreatedTime,
                                     UserName = us.UserName,
                                     SchoolName = sc.SchoolName
                                 };
                    var totalrow = record.Count();

                    var temp = record.OrderUsingSortExpression(jtSorting).
                         Skip(jtStartIndex).Take(jtPageSize);

                    return Json(new { Result = "OK", Records = temp.ToList(), TotalRecordCount = totalrow });
                }
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [HttpPost]
        public JsonResult CreateComment(Comment comment)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Form is not valid! " +
                        "Please correct it and try again."
                    });
                }
                    var record = fc.db.Comments.Add(comment);
                    fc.db.SaveChanges();
                    return Json(new { Result = "OK", Record = record });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [HttpPost]
        public JsonResult UpdateComment(Comment comment)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." });
                }
                    Comment updateComment = (from c in fc.db.Comments 
                                             where c.CommentID == comment.CommentID
                                             select c).First();

                    updateComment.CommentContent = comment.CommentContent;
                    
                    var record = fc.db.SaveChanges();

                    return Json(new { Result = "OK", Record = record });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [HttpPost]
        public JsonResult DeleteComment(Comment comment)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." });
                }
                    fc.db.Entry(comment).State = System.Data.EntityState.Deleted;

                    var record = fc.db.SaveChanges();
                    return Json(new { Result = "OK", Record = record });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }
                
        #endregion

        #region ReportedSchool - Reported Comment

        [HttpPost]
        public JsonResult ListReportedSchool(string keywords, int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                if (keywords == null)
                    keywords = "";
                var record = from sc in fc.db.Schools
                             join rs in fc.db.ReportSchools on sc.SchoolID equals rs.SchoolID
                             where rs.ReportContent.Contains(keywords)
                             select new
                             {
                                 ReportSchoolID = rs.ReportSchoolID,
                                 SchoolName = sc.SchoolName,
                                 ReportContent = rs.ReportContent,
                                 CreatedTime = rs.CreatedTime,
                                 AuthorIP = rs.AuthorIP
                             };
                var totalrow = record.Count();

                var temp = record.OrderUsingSortExpression(jtSorting).
                    Skip(jtStartIndex).Take(jtPageSize);

                return Json(new { Result = "OK", Records = temp.ToList(), TotalRecordCount = totalrow });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [HttpPost]
        public JsonResult DeleteReportedSchool(ReportSchool reportschool)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." });
                }
                fc.db.Entry(reportschool).State = System.Data.EntityState.Deleted;
                fc.db.SaveChanges();
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [HttpPost]
        public JsonResult ListReportedComment(string keywords, int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                if (keywords == null)
                    keywords = "";
                var record = from cm in fc.db.Comments
                             join us in fc.db.Users on cm.UserID equals us.UserID
                             join sc in fc.db.Schools on cm.SchoolID equals sc.SchoolID
                             where cm.TimeOfReport > 1
                             where cm.CommentContent.Contains(keywords)
                             select new
                             {
                                 CommentID = cm.CommentID,
                                 CommentContent = cm.CommentContent,
                                 CreatedTime = cm.CreatedTime,
                                 UserName = us.UserName,
                                 SchoolName = sc.SchoolName
                             };
                var totalrow = record.Count();

                var temp = record.OrderUsingSortExpression(jtSorting).
                    Skip(jtStartIndex).Take(jtPageSize);

                return Json(new { Result = "OK", Records = temp.ToList(), TotalRecordCount = totalrow });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }
        #endregion
        
        #region Another function
        // follow add new school
        [HttpGet]
        public JsonResult GetDistrictOptions(int cityId)
        {
            try
            {
                var record = fc.db.Districts.Where(d => d.CityID == cityId).Where(d => d.DistrictID != 1)
                    .Select(d => new { DisplayText = d.DistrictName, Value = d.DistrictID });
                return Json(new { Result = "OK", Options = record }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }


        // for display
        [HttpPost]
        public JsonResult GetDistrictOptions()
        {
            try
            {
                var record = fc.db.Districts.Select(d => new { DisplayText = d.DistrictName, Value = d.DistrictID });
                return Json(new { Result = "OK", Options = record });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        // for option City
        [HttpPost]
        public JsonResult GetCityOptions()
        {
            try
            {
                var record = fc.db.Cities.Where(c => c.Districts.Count > 0).Select(c => new { DisplayText = c.CityName, Value = c.CityID });
                return Json(new { Result = "OK", Options = record });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [HttpGet]
        public JsonResult GetCityOptionsByGet()
        {
            try
            {
                var record = fc.db.Cities.Where(c => c.Districts.Count > 0).Select(c => new { DisplayText = c.CityName, Value = c.CityID });
                return Json(new { Result = "OK", Options = record }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError }, JsonRequestBehavior.AllowGet);
            }
        }

        // for option SchooType
        [HttpPost]
        public JsonResult GetSchoolTypeOptions()
        {
            try
            {
                var record = fc.db.SchoolTypes.Where(st => st.TypeID != 1).Select(st => new { DisplayText = st.TypeName, Value = st.TypeID });
                return Json(new { Result = "OK", Options = record });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [HttpGet]
        public JsonResult GetSchoolTypeOptionsByGet()
        {
            try
            {
                var record = fc.db.SchoolTypes.Where(st => st.TypeID != 1).Select(st => new { DisplayText = st.TypeName, Value = st.TypeID });
                return Json(new { Result = "OK", Options = record },JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError },JsonRequestBehavior.AllowGet);
            }
        }

        //
        [HttpPost]
        public JsonResult GetServiceCheckList()
        {
            try
            {
                var record = fc.db.Services.Where(s => s.ServiceID !=1)
                    .Select(st => new { DisplayText = st.ServiceName, Value = st.ServiceID });
                return Json(new { Result = "OK", Options = record });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError });
            }
        }

        [HttpGet]
        public JsonResult GetListService(int schoolID)
        {
            try
            {
                var record = from svr in fc.db.SchoolServices
                             where svr.SchoolID == schoolID
                             select svr.ServiceID;
                             
                return Json(new { Result = "OK", Options = record.ToList() }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError }, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpGet]
        public JsonResult GetServiceCheckListByGet()
        {
            try
            {
                var record = fc.db.Services.Where(s => s.ServiceID != 1).Select(st => new { DisplayText = st.ServiceName, Value = st.ServiceID });
                return Json(new { Result = "OK", Options = record },JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = sfcMessage.ServerError }, JsonRequestBehavior.AllowGet);
            }
        }

        #endregion
    }
}